package com.project.services;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.project.utility.MediaInfo;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class ServiceMediaPlayer extends Service {

	private static MyBinder myBinder;

	@Override
	public IBinder onBind(Intent intent) {
		if (myBinder == null)
			myBinder = new MyBinder();
		return myBinder;
	}

	@Override
	public void onCreate() {

	}

	@Override
	public void onStart(Intent intent, int startId) {

	}

	@Override
	public boolean onUnbind(Intent intent) {
		return super.onUnbind(intent);
	}

	public void onDestroy() {
	}

	private class MyBinder extends Binder implements IServiceMediaPlayer {

		private MediaPlayer mediaPlayer;
		private String lastDatasource;
		private List<MediaInfo> playList;

		public MyBinder() {
			mediaPlayer = new MediaPlayer();
			playList = new ArrayList<MediaInfo>();
			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					for (int i = 0; i < playList.size(); i++) {
						if (!playList.get(i).isCurrent())
							continue;
						if (i != (playList.size() - 1)) {
							play(playList.get(i + 1));
							return;
						}
						play(playList.get(0));
						return;
					}
				}
			});
		}

		@Override
		public void play(MediaInfo media) {
			try {
				if ("video/mp4".equals(media.getEnclosureType())) {
					Intent intentToPlayVideo = new Intent(Intent.ACTION_VIEW);
					intentToPlayVideo.setDataAndType(
							Uri.parse(media.getDatasource()), "video/*");
					intentToPlayVideo.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(intentToPlayVideo);
					return;
				}

				if (lastDatasource != null
						&& lastDatasource.equals(media.getDatasource())) {
					if (mediaPlayer.isPlaying()) {
						mediaPlayer.pause();
						return;
					} else {
						mediaPlayer.start();
						return;
					}
				}
				mediaPlayer.reset();
				mediaPlayer.setDataSource(media.getDatasource());
				mediaPlayer.prepare();
				mediaPlayer.start();
				resetCurrent();
				media.setCurrent(true);
				lastDatasource = media.getDatasource();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			media.setCurrentPosition(media.getCurrentPosition());
			media.setDuration(mediaPlayer.getDuration());
		}

		private void resetCurrent() {
			for (int i = 0; i < playList.size(); i++) {
				playList.get(i).setCurrent(false);
			}
		}

		@Override
		public void addList(MediaInfo datasource) {
			for (MediaInfo element : playList) {
				if (element.getDatasource().equals(datasource.getDatasource())) {
					return;
				}
			}
			playList.add(datasource);
		}

		@Override
		public List<MediaInfo> getPlayList() {
			
			return playList;
		}

		@Override
		public void remove(MediaInfo mediaInfo) {
			playList.remove(mediaInfo);
		}

		@Override
		public MediaInfo getPlayingMedia() {
			if(playList==null||playList.size()==0)
				return null;
			for (MediaInfo element : playList) {
				if(element.isCurrent()){
					element.setCurrentPosition(mediaPlayer.getCurrentPosition());
					element.setDuration(mediaPlayer.getDuration());
					return element;
				}
			}
			return null;
		}
	}

}
